﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Factonomy.AzureUtility.Core;
using Factonomy.AzureUtility.Core.StorageHelpers;

namespace Factonomy.AzureUtility.Dialogs
{
    /// <summary>
    /// Interaction logic for ManageAccount.xaml
    /// </summary>
    public partial class ManageAccount : Window
    {
        public event NoParameterDelegate AccountChanged;

        public void OnAccountChanged()
        {
            if (AccountChanged != null)
            {
                AccountChanged();
            }
        }

        public ManageAccount(ConfigAccount account)
        {
            InitializeComponent();

            this.uiBaseAddress.Items.Add("Live");
            this.uiBaseAddress.Items.Add("Development");
            this.uiBaseAddress.SelectedIndex = 0;

            if (account != null)
            {
                this.uiAccountName.Text = account.AccountName;
                this.uiSharedKey.Text = account.SharedKey;

                if (account.Local)
                {
                    this.uiBaseAddress.SelectedItem = "Development";
                }
                else
                {
                    this.uiBaseAddress.SelectedItem = "Live";
                }
            }
        }

        private void Test(object sender, RoutedEventArgs e)
        {
            bool local = this.uiBaseAddress.SelectedItem.ToString() == "Live" ? false : true;

            //ConfigAccount account = new ConfigAccount(this.uiAccountName.Text, this.uiSharedKey.Text, local);

            BlobHelper helper = new BlobHelper();

            this.Cursor = Cursors.Wait;
            if (helper.Test(new ConfigAccount(this.uiAccountName.Text, this.uiSharedKey.Text, local, true)))
            {
                this.uiLabelResults.Foreground = Brushes.Black;
                this.uiLabelResults.Content = "Connection test was successful.";
                this.uiButtonTestConnection.IsEnabled = false;
                this.uiButtonSave.IsEnabled = true;

                this.Cursor = Cursors.Arrow;
            }
            else
            {
                this.Cursor = Cursors.Arrow;
                this.uiLabelResults.Foreground = Brushes.Red;
                this.uiLabelResults.Content = "Connection test failed.";
            }
        }

        private void Save(object sender, RoutedEventArgs e)
        {
            bool local = this.uiBaseAddress.SelectedItem.ToString() == "Live" ? false : true;
            Config.Instance.UpsertAccount(this.uiAccountName.Text, this.uiSharedKey.Text, local, true);
            this.OnAccountChanged();
            this.Close();
        }

        private void Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
